#coding=utf-8
import pygtk
pygtk.require('2.0')

import gtk, gobject
from stgui.guidb import *
from stgui.tray import StTray
from listview import StListView
import sdsource.web.sina as sina

def is_between(a, b):
    if a < b * 1.02 and a > b * 0.98:
        return True
    
    return False   
    
class EagleListView(StListView):

    def __init__(self, codes):
    
        self.cols = ['code', 'name', 'time', 'now', 'change', 'open', 'jump', 'volume']
        self.codes = codes
        
        StListView.__init__(self, self.cols, [])

    def update(self, rps=None):
        if not rps:
            rps = sina.get_rtprices(self.codes)
        
        data = []
        for rp in rps:
            code = rp.code
            name = find_stock(code).name
            d = [code, name, rp.time, rp.now, rp.change, rp.open, rp.jump, rp.volume]
            data.append(d) 
            
        model = self.get_model()       
        model.clear()
        
        for d in data:
            model.append(d)
    
    def set_codes(self, codes):
        self.codes = codes
        self.update()
            
class Eagle(object):
    
    def __init__(self):
    
        self.groups = get_stgroups()
        self.set_current_group(self.groups[0])
        
        self._init_win()
        
        vbox = gtk.VBox(False, 1)
        vbox.set_border_width(1)
        self.win.add(vbox)
  	   
        self.listview = EagleListView(self.cur_group.get_codes())
        vbox.pack_start(self.listview, False, True, 0)
        
        self.timer = gobject.timeout_add (5000, self.on_timeout)
        
        self.tray = StTray(self.win)
        
    def _init_win(self):
        win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        
        win.set_title("Eagle(鹰的眼睛) -- (%s)A股实时行情"%today)
        win.set_icon_from_file('eagle.png')
        win.set_border_width(5)
        win.set_default_size(550, 200)
        win.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        
        win.connect("destroy", gtk.main_quit)
        win.connect("window-state-event", self.on_iconfy)
        
        self.win = win
        self.win.is_iconfied = False
        
    def set_current_group(self, group):
        
        self.cur_group = group
        codes = self.cur_group.get_codes()
         
        self.is_warnned = {}
        for c in codes:
            self.is_warnned[c] = False
            
        if hasattr(self, 'listview'):
            self.listview.set_codes(codes)
            
    def on_iconfy(self, win, event):
        state = event.new_window_state
        if state == gtk.gdk.WINDOW_STATE_ICONIFIED:
            self.win.is_iconfied = True
            win.hide()
    
    def on_timeout(self):
        #print 'updating...'
        codes = self.cur_group.get_codes()
        rps = sina.get_rtprices(codes)
        
        #更新价格
        is_visibled = self.win.get_property('visible')
        if is_visibled:
            self.listview.update(rps)
        
        #预警
        for rp in rps:
            c = rp.code
            s = self.cur_group.get_mystock(c)
            now = rp.now
            
            r = False
            for p in s.pps:
                if is_between(now, p.value):
                    r = True
                    break
                        
            if r and (not self.is_warnned[c]):
                    
                self.is_warnned[c] = True
                msg = '%s(%s): %s 价格进入警戒区'%(s.name, s.code, now)
                self.tray.warnning(msg)
            
            elif (not r) and self.is_warnned[c]:
                     
                self.is_warnned[c] = False
                msg = '%s(%s): %s 价格离开警戒区'%(s.name, s.code, now)
                self.tray.warnning(msg)
                    
        return True
    
    def main(self):
        self.win.show_all()
        gtk.main()
        
if __name__ == '__main__':
    Eagle().main()
